<?php

namespace app\common\model;


use think\Model;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 数据层模型
 */
class Favorites extends Model
{
    public $page_info;

    /**
     * 收藏列表
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @param string $field 查询字段
     * @param int $page 分页信息
     * @param string $order 排序
     * @return array
     */
    public function getFavoritesList($condition, $field = '*', $page = 0, $order = 'favlog_id desc')
    {
        if ($page) {
        $res= db('favorites')->where($condition)->field($field)->order($order)->paginate($page,false,['query' => request()->param()]);
        $this->page_info=$res;
        return $res->items();
        } else {
            return db('favorites')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 收藏商品列表
     * @access public
     * @author csdeshang
     * @param array $condition 检索条件
     * @param string $field 字段
     * @param int $page 分页信息
     * @param string $order 排序
     * @return array
     */
    public function getGoodsFavoritesList($condition, $field = '*', $page = 0, $order = 'favlog_id desc')
    {
        $condition['fav_type'] = 'goods';
        return $this->getFavoritesList($condition, $field, $page, $order);
    }

    /**
     * 收藏店铺列表
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @param string $field 字段
     * @param int $page 分页信息
     * @param string $order 排序
     * @return array
     */
    public function getStoreFavoritesList($condition, $field = '*', $page = 0, $order = 'favlog_id desc')
    {
        $condition['fav_type'] = 'store';
        return $this->getFavoritesList($condition, $field, $page, $order);
    }

    /**
     * 取单个收藏的内容
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @return array 数组类型的返回结果
     */
    public function getOneFavorites($condition)
    {
        return db('favorites')->where($condition)->find();
    }

    /**
     * 获取店铺收藏数
     * @access public
     * @author csdeshang
     * @param int $storeId 店铺ID
     * @param int $memberId 会员ID
     * @return int
     */
    public function getStoreFavoritesCountByStoreId($storeId, $memberId = 0)
    {
        $where = array(
            'fav_type' => 'store', 'fav_id' => $storeId,
        );

        if ($memberId > 0) {
            $where['member_id'] = (int)$memberId;
        }

        return (int)db('favorites')->where($where)->count();
    }

    /**
     * 获取商品收藏数
     * @access public
     * @author csdeshang
     * @param int $goodsId 商品ID
     * @param int $memberId 会员ID
     * @return int
     */
    public function getGoodsFavoritesCountByGoodsId($goodsId, $memberId = 0)
    {
        $where = array(
            'fav_type' => 'goods', 'fav_id' => $goodsId,
        );

        if ($memberId > 0) {
            $where['member_id'] = (int)$memberId;
        }

        return (int)db('favorites')->where($where)->count();
    }

    /**
     * 新增收藏
     * @access public
     * @author csdeshang
     * @param array $data 参数内容
     * @return bool 布尔类型的返回结果
     */
    public function addFavorites($data)
    {
        if (empty($data)) {
            return false;
        }
        if ($data['fav_type'] == 'store') {
            $store_id = intval($data['fav_id']);
            $store_model = model('store');
            $store = $store_model->getStoreInfoByID($store_id);
            $data['store_name'] = $store['store_name'];
            $data['store_id'] = $store['store_id'];
            $data['storeclass_id'] = $store['storeclass_id'];
        }
        if ($data['fav_type'] == 'goods') {
            $goods_id = intval($data['fav_id']);
            $goods_model = model('goods');
            $goods = $goods_model->getGoodsInfoByID($goods_id);
            $data['goods_name'] = $goods['goods_name'];
            $data['goods_image'] = $goods['goods_image'];
            $data['favlog_price'] = $goods['goods_promotion_price'];//商品收藏时价格
            $data['favlog_msg'] = $goods['goods_promotion_price'];//收藏备注，默认为收藏时价格，可修改
            $data['gc_id'] = $goods['gc_id'];

            $store_id = intval($goods['store_id']);
            $store_model = model('store');
            $store = $store_model->getStoreInfoByID($store_id);
            $data['store_name'] = $store['store_name'];
            $data['store_id'] = $store['store_id'];
            $data['storeclass_id'] = $store['storeclass_id'];
        }
        return db('favorites')->insertGetId($data);
    }

    /**
     * 修改记录
     * @access public
     * @author csdeshang
     * @param type $condition 修改条件
     * @param type $data 修改数据
     * @return boolean
     */
    public function editFavorites($condition, $data)
    {
        if (empty($condition)) {
            return false;
        }
        if (is_array($data)) {
            $result = db('favorites')->where($condition)->update($data);
            return $result;
        }
        else {
            return false;
        }
    }

    /**
     * 删除
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @return bool 布尔类型的返回结果
     */
    public function delFavorites($condition)
    {
        if (empty($condition)) {
            return false;
        }
        return db('favorites')->where($condition)->delete();
    }

    /**
     * 获取店铺收藏数
     * @access public
     * @author csdeshang
     * @param int $id 会员ID
     * @return int
     */
    public function getStoreFavoritesCountByMemberId($id)
    {
        $where = array(
            'fav_type' => 'store',

        );

        if ($id > 0) {
            $where['member_id'] = (int)$id;
        }

        return (int)db('favorites')->where($where)->count();
    }
    /**
     * 获取商品收藏数
     * @access public
     * @author csdeshang
     * @param int $id 会员ID
     * @return int
     */
    public function getGoodsFavoritesCountByMemberId($id)
    {
        $where = array(
            'fav_type' => 'goods',

        );

        if ($id > 0) {
            $where['member_id'] = (int)$id;
        }

        return (int)db('favorites')->where($where)->count();
    }




    /**
     * 收藏列表
     * @access public
     * @author csdeshang
     * @param array $condition 查询条件
     * @param string $field 查询字段
     * @param int $page 分页信息
     * @param string $order 排序
     * @return array
     */
    public function getFavoriteList($member_id, $fav_type, $page = 0,$field = '*',  $order = 'favlog_id desc')
    {
        $condition['member_id'] = $member_id;
        $condition['fav_type'] = $fav_type;
        $condition['f.status'] = 0;
        if ($page) {
            if($fav_type == 1){
                //商品
                $res= db('favorites')->alias('f')
                ->join('goods g','g.goods_id = f.fav_id')
                ->join('brand b','b.brand_id = g.brand_id')
                ->where($condition)->field('f.*,b.brand_name,g.gu_final_price as price')->order($order)->paginate($page,false,['query' => request()->param()]);
                $this->page_info=$res;
                return $res->items();
            }else{
                //项目定制
                $res= db('favorites')->alias('f')
                ->join('goodsproject p','p.project_id = f.fav_id')
                ->where($condition)->field('f.*,p.*')->order($order)->paginate($page,false,['query' => request()->param()]);
                $this->page_info=$res;
                return $res->items();
            }
        } else {
            return db('favorites')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 商品收藏/取消收藏
     * @access public
     * @author csdeshang
     * @param int $id 会员ID
     * @return int
     */
    public function getGoodsCollection($member_id,$goods_id,$user_info,$fav_type)
    {
        //首先查有没有商品信息
        $goods_info = db('goods')->where(['goods_id'=>$goods_id])->find();
        if(empty($goods_info)){
            return json_fail_return('暂无商品信息');
        }
        //获取店铺信息
        $store_info = db('store')->where(['store_id'=>$goods_info['store_id']])->find();

        //判断收藏表是否有数据
        $collection_info = db('favorites')->where(['fav_type'=>$fav_type,'fav_id'=>$goods_id,'member_id'=>$member_id])->find();
        
        if (empty($collection_info)) {
            //如果没有数据的话，新增一条收藏数据
            $data = [
               'member_id' => $member_id,
               'member_name' => $user_info['member_name'],
               'fav_id' => $goods_id,
               'fav_type' => $fav_type,
               'fav_time' => time(),
               'store_id' => $goods_info['store_id'],
               'store_name' => $goods_info['store_name'],
               'storeclass_id' => $store_info['storeclass_id'],
               'goods_name' => $goods_info['goods_name'],
               'goods_image' => $goods_info['goods_image'],
               'gc_id' => $goods_info['gc_id'],
               'favlog_price' => $goods_info['goods_price'],
            ];
            $result = db('favorites')->insertGetId($data);
        }else{
            //如果有数据的话，判断是否收藏 修改状态
            if($collection_info['status'] == 0){
               $result = db('favorites')->where(['favlog_id'=>$collection_info['favlog_id']])->update(['status'=>1]);
            }else{
               $result = db('favorites')->where(['favlog_id'=>$collection_info['favlog_id']])->update(['status'=>0]);
            }
        }
        return $result;
    }


    /**
     * 项目定制收藏/取消收藏
     * @access public
     * @author csdeshang
     * @param int $id 会员ID
     * @return int
     */
    public function getProjectCollection($member_id,$project_id,$user_info,$fav_type)
    {
        //首先查有没有项目定制信息
        $project_info = db('goodsproject')->where(['project_id'=>$project_id])->find();
        if(empty($project_info)){
            return json_fail_return('暂无项目定制信息');
        }

        //判断收藏表是否有数据
        $collection_info = db('favorites')->where(['fav_id'=>$project_id,'member_id'=>$member_id,'fav_type'=>2])->find();
        
        if (empty($collection_info)) {
            //如果没有数据的话，新增一条收藏数据
            $data = [
               'member_id' => $member_id,
               'member_name' => $user_info['member_name'],
               'fav_id' => $project_id,
               'fav_type' => $fav_type,
               'fav_time' => time(),
               'goods_name' => $project_info['project_title'],
               'goods_image' => $project_info['project_images'],
               'favlog_price' => $project_info['total_price'],
            ];
            $result = db('favorites')->insertGetId($data);
            //增加当前项目定制的收藏量
            $collection_num = $project_info['love_num'] + 1;
        }else{
            //如果有数据的话，判断是否收藏 修改状态
            if($collection_info['status'] == 0){
               $result = db('favorites')->where(['favlog_id'=>$collection_info['favlog_id']])->update(['status'=>1]);
                //减少当前项目定制的收藏量
                $collection_num = $project_info['love_num'] - 1;
            
            }else{
               $result = db('favorites')->where(['favlog_id'=>$collection_info['favlog_id']])->update(['status'=>0]);
                //增加当前项目定制的收藏量
                $collection_num = $project_info['love_num'] + 1;
            }
        }
        db('goodsproject')->where(['project_id'=>$project_id])->update(['love_num'=>$collection_num]);  
        return $result;
    }

    
}